package com.emm.yixun.aop;
import java.lang.reflect.Method;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.alibaba.druid.util.StringUtils;
import com.emm.yixun.anotations.UserOperateLog;
import com.emm.yixun.common.enums.SourceTypeEnum;
import com.emm.yixun.common.model.ProjectRoom;
import com.emm.yixun.common.model.User;
import com.emm.yixun.common.model.dto.LogDto;
import com.emm.yixun.common.model.enums.DelFlagEnum;
import com.emm.yixun.common.response.Response;
import com.emm.yixun.estate.service.ProjectRoomServiceFacade;
import com.emm.yixun.system.service.LogServiceFacade;
import com.emm.yixun.system.service.UserServiceFacade;
import com.emm.yixun.website.common.utils.security.ShiroUtil;
import com.emm.yixun.website.controller.base.BaseController;

@Aspect    
@Component    
public class UserOperateLogAdvisor extends BaseController<Object>{
	
	@Autowired
	private LogServiceFacade logServiceFacade;
	@Autowired
	private UserServiceFacade userServiceFacade;
	@Autowired
	private ProjectRoomServiceFacade projectRoomServiceFacade;
	
    @Pointcut("@annotation(com.emm.yixun.anotations.UserOperateLog)")    
	public void controllerAspect(){
	
	}
    @Before("controllerAspect()")    
    public  void doBefore(JoinPoint joinPoint) { 
    	 try {
    		 //根据projectKey获得projectId
    		 HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
    		 //获得注解
    		 Method method = ((MethodSignature) joinPoint.getSignature()).getMethod();
    		 UserOperateLog operateLog = method.getAnnotation(UserOperateLog.class);
    		 User userInfo = null;
    		 String projectId = null;
    		 //是登录接口
    		 if(operateLog.isLogin()){
    			 //根据登录
    			 String username =  request.getParameter("username");
    			 String password =  request.getParameter("password");
    			 Response<User> response=userServiceFacade.login(null, username, password);
    			 if(response.isSuccess()){
    				 userInfo = response.getEntity();
    			 }
    		 }else{
    			 if(!"".equals(operateLog.projectKey())&&operateLog.projectKey()!=null){
    				 projectId = request.getParameter(operateLog.projectKey());
    			 }else if(!"".equals(operateLog.roomKey())&&operateLog.roomKey()!=""){
    				 String roomId =request.getParameter(operateLog.roomKey());
    				 Response<ProjectRoom> response =projectRoomServiceFacade.findById(new Long(roomId));
    				 if(response.isSuccess()){
    					 projectId=response.getEntity().getProjectId().toString();
    				 }
    			 }
    			 userInfo=ShiroUtil.getCurrentUser();
    		 }
    		 if(userInfo!=null){
    			 LogDto logDto = new LogDto();
        		 logDto.setMerchantId(userInfo.getMerchantId());
        		 logDto.setCreateUserId(userInfo.getId());
        		 logDto.setCreateTime(new Date());
        		 logDto.setUpdateTime(new Date());
        		 logDto.setDelFlag(DelFlagEnum.UNDEL.getNo());
        		 logDto.setProjectId(projectId==null?null:new Long(projectId));
        		 logDto.setModelName(operateLog.modelName().getModelName());
        		 logDto.setFunctionName(operateLog.functionName().getFunctionTypeName());
        		 logDto.setSourceType(SourceTypeEnum.SOURCETYPE_0.getSourceType());
        		 logServiceFacade.save(logDto);
    		 }
    		
		} catch (Exception e) {
			logger.error("doBefore error: ", e);
		}
    }
    public static void main(String[] args) {
		System.out.println(!StringUtils.isEmpty("123"));
	}
}
